/*
 * @Author: zhangwei 1405298045@qq.com
 * @Date: 2022-03-04 14:47:05
 * @LastEditTime: 2022-03-12 11:15:08
 * @LastEditors: zhangwei 1405298045@qq.com
 * @Description:
 * @FilePath: /ll-fe-mp-dyfx/src/store/modules/srorage.js
 *
 */
const HISTORY_MAX_LENGTH = 20;
import srorageKey from '@/utils/srorage/key';
import { getLocal, setLocal } from '@/utils';

export default {
	state: {
		incomeStatus: getLocal(srorageKey.incomeStatus),

		currentSite: getLocal(srorageKey.currentSite) || {},

		historySite: getLocal(srorageKey.historySite) || [],

		tandianSearchHistory: getLocal(srorageKey.tandianSearchHistory) || [],
		materialSearchHistory: getLocal(srorageKey.materialSearchHistory) || []
	},

	getters: {
		currentSite({ currentSite }) {
			return currentSite;
		},

		isIncomeStatus({ incomeStatus }) {
			return incomeStatus;
		},

		historySite({ historySite }) {
			return historySite;
		},

		tandianSearchHistory({ tandianSearchHistory }) {
			return tandianSearchHistory;
		},

		materialSearchHistory({ materialSearchHistory }) {
			return materialSearchHistory;
		}
	},

	mutations: {
		changeIncomeStatus(state, status) {
			const currentStatus = !state.incomeStatus;

			state.incomeStatus = currentStatus;
			setLocal(srorageKey.incomeStatus, currentStatus);
		},

		changeCurrentSite(state, params) {
			state.currentSite = params;

			setLocal(srorageKey.currentSite, params);
		},

		changeHistorySite(state, params) {
			let browseSiteList = state.historySite;

			let filtersArray = [];
			const index = browseSiteList.findIndex((item) => item.id === params.id);

			if (!~index) {
				if (browseSiteList.length === 3) {
					filtersArray = [params, ...browseSiteList.slice(0, 2)];
				} else {
					filtersArray = [params, ...browseSiteList];
				}
			} else {
				browseSiteList.splice(index, 1);
				filtersArray = [params, ...browseSiteList];
			}

			state.historySite = filtersArray;
			setLocal(srorageKey.historySite, filtersArray);

			this.commit('changeCurrentSite', params);
		},

		changeTandianSearchHistory(state, params) {
			let history = state.tandianSearchHistory;

			if (!params) {
				state.tandianSearchHistory = [];
				setLocal(srorageKey.tandianSearchHistory, []);

				return;
			}

			const index = history.findIndex((item) => item === params);

			if (!~index) {
				history.length >= HISTORY_MAX_LENGTH && history.pop();
			} else {
				history.splice(index, 1);
			}

			history.unshift(params);

			state.tandianSearchHistory = history;
			setLocal(srorageKey.tandianSearchHistory, history);
		},

		changeMaterialSearchHistory(state, params) {
			let history = state.materialSearchHistory;

			if (!params) {
				state.materialSearchHistory = [];
				setLocal(srorageKey.materialSearchHistory, []);

				return;
			}

			const index = history.findIndex((item) => item === params);

			if (!~index) {
				history.length >= HISTORY_MAX_LENGTH && history.pop();
			} else {
				history.splice(index, 1);
			}

			history.unshift(params);

			state.materialSearchHistory = history;
			setLocal(srorageKey.materialSearchHistory, history);
		}
	},

	actions: {}
};
